﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class Backend : System.Web.UI.MasterPage
    {
        protected static string _idusername;
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie LangBackend = Request.Cookies["Lang_Backend"];
            if (!IsPostBack)
            {
                try
                {
                    if (Session["UserName"].ToString().Trim().Equals("") || Session["UserName"].ToString().Trim().Equals(null) || Session["ID_UserName"].ToString().Trim().Equals("") || Session["ID_UserName"].ToString().Trim().Equals(null))
                    {
                        Response.Redirect("Login.aspx");
                    }
                    else
                    {
                        string urlcurrent = Request.AppRelativeCurrentExecutionFilePath.Substring(10).ToString().Trim();

                        DataTable dturl = Phoenix.AppCode.Bussiness.UsersService.UsersMenuListCheck(Session["ID_UserName"].ToString(), urlcurrent);
                        if (dturl.Rows.Count > 0)
                        {
                            _idusername = Session["ID_UserName"].ToString();

                            DataTable dt = Phoenix.AppCode.Bussiness.UsersService.UserMenuListControl(Session["ID_UserName"].ToString(), Request.Cookies["Lang_Backend"].Value);
                            if (dt.Rows.Count > 0)
                            {
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    if (dt.Rows[i]["ID_MenuList"].ToString() != "7")
                                    {
                                        lblMenuList.Text += "<li><a href='" + dt.Rows[i]["URL"].ToString() + "'><img src='http://localhost:1109/Content/Backend/images/icon_bullet.png' alt='Pages'/>" +
                                                                            dt.Rows[i]["MenuName"].ToString() + "</a></li>";
                                    }
                                }
                            }

                            if (LangBackend == null)
                            {
                                LangBackend = new HttpCookie("Lang_Backend", "vi-VN");
                                LangBackend.Expires = DateTime.Now.AddDays(1);
                                Response.Cookies.Add(LangBackend);
                            }
                            else
                            {
                                if (LangBackend.Value == "vi-VN")
                                {
                                    LangBackend = new HttpCookie("Lang_Backend", "vi-VN");
                                    LangBackend.Expires = DateTime.Now.AddDays(1);
                                    Response.Cookies.Add(LangBackend);
                                }
                                else
                                {
                                    LangBackend = new HttpCookie("Lang_Backend", "en-US");
                                    LangBackend.Expires = DateTime.Now.AddDays(1);
                                    Response.Cookies.Add(LangBackend);
                                }
                            }
                        }
                        else
                        {
                            Response.Redirect("Login.aspx");
                        }
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }

        protected void ibtnLang_vi_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie LangBackend = new HttpCookie("Lang_Backend", "vi-VN");
            LangBackend.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(LangBackend);
            Response.Redirect(Request.Url.AbsoluteUri);
        }

        protected void ibtnLang_en_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie LangBackend = new HttpCookie("Lang_Backend", "en-US");
            LangBackend.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(LangBackend);
            Response.Redirect(Request.Url.AbsoluteUri);
        }

        protected void lbtnLogout_Click(object sender, EventArgs e)
        {
            Phoenix.AppCode.Bussiness.UsersService.UsersLastLogout(Session["ID_UserName"].ToString());
            Session.Remove("UserName");
            Session.Remove("ID_UserName");
            Response.Redirect("Login.aspx");
        }
    }
}